<template>
  <div class="cardWrapper" :id="`menuScroll${scrollFirstIndex}-${componentsIndex}`" v-if="layerItem.children.length">
    <div class="cardTitleContainer">
      <img src="https://img3.job1001.com/data_center/images/position_item_icon.png" alt="Icon"
        class="position_item_icon" />
      <div class="cardTitle">{{ layerItem.title }}</div>
    </div>
    <div class="layerList">
      <el-row class="demo-autocomplete" :key="index" v-for="item,index in layerItem.children||layerList">
        <div class="demo-autocomplete-content">
          <div class="controlLine" :id="`scroll${componentsIndex}-${index}`">
            <span class="tinyTitle">
              <div class="titleFlag"></div>
              {{ item.title }}
            </span>
            <div class="line"></div>
          </div>
          <template v-if="item.layerId==159">
            <div class="interviewQAList" v-if="interviewQAList&&interviewQAList.length">
              <div class="interviewQAItem" v-for="item,index in interviewQAList" :key="index">
                <div class="title">
                  <div class="askIcon">问</div> {{item.title}}
                </div>
                <p class="standardAnswer">{{item.standardAnswer}}</p>
              </div>
            </div>
            <el-empty class="empty" description="暂未上线，敬请期待~" v-else></el-empty>
          </template>
          <template>
            <div class="caseTableContainer" v-loading="loadings[item.layerId]" v-if="item.layerId==200">
              <!-- <div class="caseTableForm">
                <el-form ref="form" :model="forms[item.layerId]" inline>
                  <el-form-item label="专业:" :style="{ width: '240px' }">
                    <el-select v-model="forms[item.layerId].tagId" :style="{ width: '185px' }" size="small" filterable
                      placeholder="全部" @change="(e)=>{handleMajorChange(e,item.layerId)}">
                      <el-option v-for="item,index in majorList" :key="index" :label="item.tagName"
                        :value="item.positionTagId">
                      </el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="学校:" :style="{ width: '170px' }">
                    <el-select v-model="forms[item.layerId].collegeId" :style="{ width: '115px' }" size="small"
                      filterable placeholder="请选择" @change="(e)=>{handleCollegeChange(e,item.layerId)}">
                      <el-option v-for="item,index in collegeList" :key="index" :label="item.label" :value="item.value">
                      </el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="就业区域:" :style="{ width: '190px' }">
                    <el-select v-model="forms[item.layerId].regionId" :style="{ width: '115px' }" size="small"
                      filterable placeholder="全部" @change="(e)=>{handleRegionChange(e,item.layerId)}">
                      <el-option v-for="item,index in regionLists[item.layerId]" :key="index" :label="item.relDesc"
                        :value="item.dataId">
                      </el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="更新时间:" :style="{ width: '250px' }">
                    <el-date-picker v-model="selectBetweenTimes[item.layerId]" :style="{ width: '176px' }" size="small"
                      type="monthrange" align="right" value-format="yyyy-MM-dd HH:mm:ss" format="yyyy.MM"
                      start-placeholder="开始月份" end-placeholder="结束月份" :picker-options="pickerOptions"
                      @change="(e)=>{handleUpdateTimeChange(e,item.layerId)}">
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item>
                    <el-button plain type="primary" size="small" @click="resetPage(item.layerId)"> 查询</el-button>
                  </el-form-item>
                </el-form>
              </div> -->
              <div class="caseTableList" v-if="caseTableList[item.layerId]&&caseTableList[item.layerId].length">
                <template>
                  <div class="caseTableItem" v-for="_item,_index in caseTableList[item.layerId]"
                    :key="item.layerId+''+_index">
                    <div class="caseTableItemImageContainer" @click="handleCaseItemClick(_item)">
                      <img :src="_item.picture" alt="" class="caseTableItemImage">
                      <img src="@/assets/detail/pdfTypeIcon.png" class="caseTableItemIcon"
                        v-if="_item.fileType=='PDF'" />
                      <img src="@/assets/detail/wordTypeIcon.png" class="caseTableItemIcon"
                        v-else-if="_item.fileType=='Word'" />
                      <img src="@/assets/detail/pptTypeIcon.png" class="caseTableItemIcon"
                        v-else-if="_item.fileType=='PowerPoint'" />
                    </div>
                    <p class="caseTableItemName" @click="handleCaseItemClick(_item)">{{_item.fileName}}</p>
                    <div class="caseTableItemBottom">
                      <span class="caseTableItemDate">{{_item.fileDesc}}</span>
                      <div class="caseTableItemButtons">
                        <template>
                          <img v-if="_item.collect" @click="reqUpdateCollect(_item,0,item.layerId)" class="collectIcon"
                            src="https://img3.job1001.com/data_center/images/caseTableItemLiked.svg" />
                          <img v-else @click="reqUpdateCollect(_item,1,item.layerId)" class="collectIcon"
                            src="https://img3.job1001.com/data_center/images/caseTableItemLike.png" />
                        </template>
                        <template>
                          <img @click="handleDownloadClick(_item)" class="downloadBtnIcon"
                            src="https://img3.job1001.com/data_center/images/caseDownload.svg" />
                          <div class="downloadBtn" @click="handleDownloadClick(_item)">下载</div>
                        </template>
                      </div>
                    </div>
                  </div>
                </template>
              </div>
              <el-empty class="empty" description="暂未上线，敬请期待~" v-else></el-empty>
              <div class="caseTablePagination">
                <el-pagination layout="total, sizes, prev, pager, next, jumper"
                  :current-page="tablePages[item.layerId].pageNo" :page-size="tablePages[item.layerId].pageSize"
                  :page-sizes="tablePages[item.layerId].pageSizes" :total="tablePages[item.layerId].total"
                  @size-change="handleSizeChange" @current-change="(e)=>{handleCurrentChange(e,item.layerId)}" />
                </el-pagination>
              </div>
            </div>
          </template>
        </div>
      </el-row>
    </div>
    <officePreviewDialog v-if="officePreviewVisible" :dialogVisible.sync="officePreviewVisible" :fileName="fileName"
      :fileSrc="fileSrc">
    </officePreviewDialog>
  </div>
</template>

<script>
import {
  campusQuestions,
  campusFiles,
  campusCollect,
} from "@/api/position-detail.js";
import officePreviewDialog from "@/components/OfficePreviewDialog";
import { download } from "@/utils/fileSaver";
export default {
  name: "JobInterview",
  components: {
    officePreviewDialog,
  },
  props: {
    layerList: {
      type: Array,
      default: () => [],
    },
    layerItem: {
      type: Object,
      default: () => {},
    },
    topId: {
      type: String,
      required: true,
    },
    isSuperToken: {
      type: Boolean,
      default: false,
    },
    scrollFirstIndex: {
      type: [String, Number],
      default: 0,
    },
    aiCreateStatus: {
      type: Boolean,
      default: false,
    },
    isAdmin: {
      type: Boolean,
      default: false,
    },
    editAll: {
      type: Boolean,
      default: false,
    },
  },
  watch: {
    layerItem: {
      handler(newValue, oldValue) {
        if (newValue.layerId) {
          this.layerList.forEach((item, index) => {
            if (item.layerId == newValue.layerId) {
              this.componentsIndex = index;
            }
          });
        }
      },
      immediate: true,
    },
  },
  data() {
    return {
      officePreviewVisible: false,
      fileName: "",
      fileSrc: "",
      positionId: this.$route.query.id,
      componentsIndex: "", //用来标记组件下标，滚动用
      layerDataList: [],
      interviewQAList: [],
      caseTableList: { 200: [] },
      regionLists: [],
      layerIdContentMap: {
        200: 4,
      },
      forms: {
        200: {
          tagId: "",
          collegeId: " ",
          regionId: "",
          startTime: "",
          endTime: "",
        },
      },
      tablePages: {
        200: {
          pageNo: 1, // 第几页
          pageSize: 10, // 每页多少条
          pageSizes: [10, 20, 30], // 每页多少条
          total: 0, // 总记录数
        },
      },
      selectBetweenTimes: {
        200: [],
      },
      loadings: {
        200: false,
      },
    };
  },
  created() {
    this.reqGetQuestionsList();

    this.reqGetResumeTemplateList(200);
  },
  methods: {
    determineFileType(fileUrl) {
      const fileExtension = fileUrl.split(".").pop().toLowerCase();

      const fileTypes = {
        pdf: "PDF",
        doc: "Word",
        docx: "Word",
        ppt: "PowerPoint",
        pptx: "PowerPoint",
        xls: "Excel",
        xlsx: "Excel",
      };
      if (fileTypes[fileExtension]) {
        return fileTypes[fileExtension];
      } else {
        return "Unknown File Type";
      }
    },
    //获取案例列表
    reqGetResumeTemplateList(layerId) {
      const type = this.layerIdContentMap[layerId];
      this.$set(this.loadings, layerId, true);
      const params = {
        ...this.forms[layerId],
        pageNo: this.tablePages[layerId].pageNo,
        pageSize: this.tablePages[layerId].pageSize,
        // positionId: this.$route.query.id,
        type,
        dataType: 4,
      };
      campusFiles(params)
        .then((res) => {
          if (res.code == 200) {
            switch (type) {
              case 4:
                this.caseTableList[layerId] = res.data.map((item) => {
                  item.fileType = this.determineFileType(item.fileUrl);
                  return item;
                });
                this.tablePages[layerId].total = res.pageparam.sums;
                break;

              default:
                break;
            }
          } else {
            this.$message.error(res.status_desc || "服务器开小差啦");
          }
        })
        .catch((e) => {
          console.log(e);
        })
        .finally(() => {
          this.$set(this.loadings, layerId, false);
        });
    },
    handleCaseItemClick(item) {
      this.fileSrc = item.fileUrl;
      this.fileName = item.fileName;
      this.officePreviewVisible = true;
    },
    reqUpdateCollect(item, action, layerId) {
      if (!item.fileId) {
        return;
      }

      //console.log("before item.collect", item.collect);
      const params = {
        dataId: item.videoId || item.fileId,
        dataType: 4, //1视频 2毕业案例
        action, //1新增收藏  0取消收藏
        type: this.layerIdContentMap[layerId],
      };
      let message;
      switch (action) {
        case 1:
          message = "收藏";
          break;
        case 0:
          message = "取消收藏";
          break;

        default:
          break;
      }
      campusCollect(params)
        .then((res) => {
          if (res.code == 200) {
            this.$message.success(message + "成功！");
            this.$set(item, "collect", action);
          } else {
            this.$message.error(message + "失败！");
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },
    handleDownloadClick(item) {
      const suffixList = [".pdf", ".doc", "docx", "xls", "xlsx", "ppt", "pptx"];
      const suffix = suffixList.includes(item.fileUrl.slice(-4))
        ? item.fileUrl.slice(-4)
        : ".pdf";
      download(item.fileUrl, item.fileName + suffix);
    },
    reqGetQuestionsList() {
      this.loading = true;
      const params = {
        positionId: this.$route.query.id,
        tagLabel: "初级",
        pageNo: 1,
        pageSize: 10,
      };
      campusQuestions(params)
        .then((res) => {
          if (res.code == 200) {
            this.interviewQAList = res.data;
            // this.tablePage.total = res.pageparam.sums;
          } else {
            this.$message.error(res.status_desc || "服务器开小差啦");
          }
        })
        .catch((e) => {
          console.log(e);
        })
        .finally(() => {
          this.loading = false;
        });
    },
    formatDate(date) {
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, "0");
      const day = String(date.getDate()).padStart(2, "0");
      const hours = String(date.getHours()).padStart(2, "0");
      const minutes = String(date.getMinutes()).padStart(2, "0");
      const seconds = String(date.getSeconds()).padStart(2, "0");
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },
    handleSizeChange(val, layerId) {
      this.tablePages[layerId].pageNo = 1;
      this.reqGetResumeTemplateList(layerId);
    },
    resetPage(layerId) {
      this.tablePages[layerId].pageNo = 1;
      this.reqGetResumeTemplateList(layerId);
    },
    handleCurrentChange(val, layerId) {
      this.tablePages[layerId].pageNo = val;
      this.reqGetResumeTemplateList(layerId);
    },
  },
};
</script>

<style lang="scss" scoped>
.cardWrapper {
  // position: relative;
  min-width: 800px;
  border-radius: 10px;
  border: 1px solid #cbdcfe;
  background-size: 110% 219px;
  background-position: -10px -10px;
  background-image: url("https://img3.job1001.com/data_center/images/salary_bg.png");
  background-repeat: no-repeat;
  padding: 20px;
  flex: 1;
  margin: 0;

  margin-bottom: 14px;
  // margin-top: 20px;
  &::-webkit-scrollbar {
    width: 4px;
    background-color: #fff;
    border-radius: 4px;

    height: 4px;
  }
  &::-webkit-scrollbar-thumb {
    background-color: #d8d8d8;
    border-radius: 4px;
  }
  .cardTitleContainer {
    display: flex;
    align-items: center;
    margin-bottom: 16px;
    // position: sticky;
    top: 0;
    .cardTitle {
      height: 30px;
      font-size: 22px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 600;
      color: #000000;
      line-height: 30px;
      margin-right: 15px;
    }
    .introduceJobInput {
      width: 400px;
    }
    .introduceJob {
      height: 21px;
      font-size: 15px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #999999;
      line-height: 21px;
      margin-right: 38px;
    }
    .position_item_icon {
      width: 39px;
      height: 40px;
      margin-right: 13px;
    }
  }

  .layerList {
    .demo-autocomplete {
      display: flex;
      .demo-autocomplete-content {
        flex: 1;
      }
      .positionPic {
        width: 140px;
        // height: 137px;
        border-radius: 16px;
        opacity: 0.77;
      }
      .interviewQAList {
        margin-top: 24px;
        .interviewQAItem {
          margin-top: 28px;
          &:nth-of-type(1) {
            margin-top: 0px;
          }
          .title {
            display: flex;
            align-items: center;
            font-family: PingFang-SC, PingFang-SC;
            font-weight: 800;
            font-size: 15px;
            color: #333333;
            line-height: 26px;
            text-align: left;
            font-style: normal;
            .askIcon {
              position: relative;
              display: flex;
              align-items: center;
              justify-content: center;
              width: 17px;
              height: 18px;
              background: #00b656;
              color: white;
              margin-right: 5px;
              font-size: 11px;
              border-radius: 3px;
              &::after {
                content: "";
                position: absolute;
                bottom: -7px;
                width: 1px;
                height: 1px;
                border: 4px solid transparent;
                border-top: 4px solid #00b656;
              }
            }
          }
          .standardAnswer {
            // width: 992px;
            // height: 52px;
            font-family: PingFangSC, PingFang SC;
            font-weight: 400;
            font-size: 15px;
            color: #555555;
            line-height: 26px;
            text-align: left;
            font-style: normal;
            margin-top: 10px;
          }
        }
      }
      .caseTableContainer {
        .caseTableForm {
        }
        .caseTableList {
          display: flex;
          flex-wrap: wrap;
          // grid-template-columns: repeat(auto-fit, minmax(184px, 1fr));
          // grid-template-rows: auto; /* 根据需要调整行高 */
          // grid-column-gap: 20px;
          // grid-row-gap: 20px;
          .caseTableItem {
            position: relative;
            display: flex;
            flex-direction: column;
            width: 20%;
            // height: 343px;
            background: #f9f9f9;
            // border-radius: 8px;
            border: 2px solid transparent;
            margin: 0px;
            padding: 10px;
            &:hover {
              border: 2px solid #4880ff;
              box-shadow: 0px 4px 4px 0px rgba(0, 0, 0, 0.16);
            }

            .caseTableItemImageContainer {
              position: relative;
              &::before {
                content: "";
                position: absolute;
                top: 0;
                left: 0;
                width: 100%;
                height: 23px;
                background: linear-gradient(
                  180deg,
                  rgba(0, 0, 0, 0.08) 0%,
                  transparent 100%
                );
                border-radius: 8px 8px 0 0;
                // box-shadow: 0px 4px 4px 0px rgba(0, 0, 0, 0.16);
              }
              .caseTableItemImage {
                width: 100%;
                // height: 240px;
                color: white;
                border: 0;
                outline: 0;
                border-radius: 8px;
                box-shadow: 0 4px 2px 0px rgba(0, 0, 0, 0.08);
                cursor: pointer;
              }
              .caseTableItemIcon {
                position: absolute;
                right: 0;
                bottom: 3px;
                width: 18px;
                height: 18px;
              }
            }
            .caseTableItemName {
              height: 38px;
              font-family: PingFangSC, PingFang SC;
              font-weight: 400;
              font-size: 15px;
              color: #222222;
              line-height: 20px;
              margin: 16px 10px 10px;
              cursor: pointer;
              overflow: hidden;
              text-overflow: ellipsis;
              display: -webkit-inline-box;
              -webkit-box-orient: vertical;
              -webkit-line-clamp: 2;
            }
            .caseTableItemBottom {
              display: flex;
              justify-content: space-between;
              align-items: center;
              height: 24px;
              margin: 0px 10px 12px;
              .caseTableItemDate {
                width: 76px;
                height: 20px;
                font-family: PingFangSC, PingFang SC;
                font-weight: 400;
                font-size: 15px;
                color: #999999;
                line-height: 20px;
              }
              .caseTableItemButtons {
                display: flex;
                align-items: center;
                font-size: 0;
                // &:hover {
                //   .downloadBtnIcon {
                //     display: none;
                //   }
                //   .downloadBtn {
                //     display: flex;
                //   }
                // }
                .collectIcon {
                  cursor: pointer;
                  width: 16px;
                  height: 16px;
                  margin-right: 8px;
                }
                .downloadBtnIcon {
                  cursor: pointer;
                  width: 16px;
                  height: 16px;
                }
                .downloadBtn {
                  display: none;
                  // margin-left: 8px;
                  padding: 2px 8px;
                  background: #4880ff;
                  box-shadow: 0px 4px 4px 0px rgba(0, 0, 0, 0.16);
                  border-radius: 6px;
                  font-family: PingFangSC, PingFang SC;
                  font-weight: 400;
                  font-size: 15px;
                  color: #ffffff;
                  line-height: 20px;
                  text-align: left;
                  font-style: normal;
                  cursor: pointer;
                }
              }
            }
          }
        }
        .empty {
          flex: 1;
          min-height: 328px;
        }
        .caseTablePagination {
          display: flex;
          justify-content: flex-end;
          margin-top: 24px;
        }
      }
    }
  }

  .titleFlag {
    width: 5px;
    height: 15px;
    border-radius: 32px;
    background-color: #409eff;
    margin-right: 12px;
    color: red;
  }
  .controlLine {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    margin-bottom: 15px;
    .tinyTitle {
      display: flex;
      align-items: center;
      font-weight: bold;
      font-size: 18px;
      // margin-right: 24px;
    }
    .line {
      flex: 1;
      // height: 16px;
      margin-left: 14px;
      border-bottom: 1px solid #efefef;
    }
  }
}
</style>